'use client'

import React from 'react';
import { BaseEditorProps } from './types';

export default function TimelineEditor({ properties, updateProperty, handleFileSelect }: BaseEditorProps) {
  return (
    <div className="space-y-4">
      <div>
        <h3 className="font-medium text-gray-800 mb-3">时间轴数据</h3>
        <div className="space-y-3">
          {(properties.timelineData || []).map((item: any, index: number) => (
            <div key={index} className="border border-gray-200 rounded p-3 bg-gray-50">
              <div className="space-y-2">
                <div>
                  <label className="block text-xs text-gray-600 mb-1">标签</label>
                  <input
                    type="text"
                    className="w-full p-2 border border-gray-300 rounded text-sm"
                    value={item.label || ""}
                    onChange={(e) => {
                      const newData = [...(properties.timelineData || [])];
                      newData[index].label = e.target.value;
                      updateProperty('timelineData', newData);
                    }}
                  />
                </div>
                <div>
                  <label className="block text-xs text-gray-600 mb-1">图片URL</label>
                  <input
                    type="text"
                    className="w-full p-2 border border-gray-300 rounded text-sm"
                    value={item.img || ""}
                    onChange={(e) => {
                      const newData = [...(properties.timelineData || [])];
                      newData[index].img = e.target.value;
                      updateProperty('timelineData', newData);
                    }}
                  />
                </div>
                <div>
                  <label className="block text-xs text-gray-600 mb-1">上传图片</label>
                  <input
                    type="file"
                    accept="image/*"
                    onChange={(e) => handleFileSelect(e, { type: 'timeline', index })}
                    className="w-full text-sm file:mr-2 file:py-1 file:px-3 file:rounded file:border file:text-sm file:font-medium file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100"
                  />
                </div>
                <button
                  type="button"
                  className="text-red-500 hover:text-red-700 text-sm"
                  onClick={() => {
                    const newData = [...(properties.timelineData || [])];
                    newData.splice(index, 1);
                    updateProperty('timelineData', newData);
                  }}
                >
                  删除此项
                </button>
              </div>
            </div>
          ))}
          <button
            type="button"
            className="w-full py-2 border-2 border-dashed border-gray-300 rounded text-gray-500 hover:border-gray-400 hover:text-gray-600 text-sm"
            onClick={() => {
              const newData = [...(properties.timelineData || []), { label: "", img: "" }];
              updateProperty('timelineData', newData);
            }}
          >
            + 新增时间轴项目
          </button>
        </div>
      </div>
    </div>
  );
}
